<template>
<div class='login-page'>

     <van-nav-bar
  title="账号登录"
  left-arrow
  @click-left="onClickLeft
  "
/>

  <van-form @submit="onSubmit">
  <van-field
    v-model="username"
    name="username"
    placeholder="请输入账号"
    :rules="[
    { required: true, message: '请填写用户名' },
    // 添加正则校验
    {pattern:/^\w{5,}$/,message:'用户名至少为5位'}]"
  />
  <van-field
    v-model="password"
    type="password"
    name="password"
    placeholder="请输入密码"
    :rules="[
    { required: true, message: '请填写密码' },
   { pattern:/^\w{5,}$/,message:'密码至少为6位'}
    ]"
  />
  <div style="margin: 16px;">
    <van-button  block type="primary" native-type="submit">登录</van-button>
  </div>
</van-form>
<router-link class="link" to='/register'>还没有账号，去注册~</router-link>

</div>
</template>

<script >
import { login } from '@/api/user'
import { Toast } from 'vant'
import { setToken } from '@/utils/storage'
export default {
  name: 'login-page',
  data () {
    return {
      username: '',
      password: ''
    }
  },
  methods: {
    async onSubmit (values) {
      const res = await login(values)// 发送请求
      console.log(res)
      setToken(res.body.token)// 先存本地
      Toast.success('登录成功')// 提示
      this.$router.push('/my')// 跳转到我的页面
    },
    onClickLeft () {
      this.$router.go(-1)
    }
  }
}
</script>

<style >
.van-nav-bar__content{
  background-color: #07c160 ;

}
.van-nav-bar__title {
color: #fff;
}
.van-icon-arrow-left:before{
  color: #fff;
}
.link {
  color: #333;
  font-size: 12px;
  text-align: center;
  margin-top: 30px;
  display: block;
}

</style>
